/**
 * Filename: NativePreviewAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/3/29 16:05
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNNativeImage;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.SnackBarUtils;
import com.github.postsanf.yinian.utils.ToastUtils;
import com.github.postsanf.yinian.view.TouchImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;
import java.util.List;

public class NativePreviewAdapter extends PagerAdapter {
	private Activity context;
	private List<YNNativeImage>  picUrls = new ArrayList<>();
	private ArrayList<View> picViews;
	private ImageLoader mImageLoader;

	public NativePreviewAdapter(Activity context, List<YNNativeImage> picUrls) {
		this.picUrls.clear();
		this.context = context;
		this.picUrls.addAll(picUrls);
		this.mImageLoader = ImageLoader.getInstance();
		initImgs();
	}

	private void initImgs() {
		picViews = new ArrayList<View>();
		for(int i=0; i<picUrls.size(); i++) {
			// 填充显示图片的页面布局
			View view = View.inflate(context, R.layout.yn_item_native_preview, null);
			picViews.add(view);
		}
	}

	@Override
	public int getCount() {
		return picUrls.size();
	}
	
	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public View instantiateItem(ViewGroup container, final int position) {
		View view = picViews.get(position);
		final TouchImageView iv_native_preview = (TouchImageView) view.findViewById(R.id.iv_native_preview);

		String url = picUrls.get(position).getPath();
		if (!url.contains("http")){
			url = "file://"+Uri.parse(url);
		}
		mImageLoader.displayImage(url, new ImageViewAware(iv_native_preview, false),
				ImageOptHelper.getImgOptions(),new SimpleImageLoadingListener(){
					@Override
					public void onLoadingStarted(String imageUri, View view) {
					}
					@Override
					public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
						String message = null;
						switch (failReason.getType()) {     // 获取图片失败类型
							case IO_ERROR:              // 文件I/O错误
								message = "文件错误";
								break;
							case DECODING_ERROR:        // 解码错误
								message = "解码错误";
								break;
							case NETWORK_DENIED:        // 网络延迟
								message = "网络延迟";
								break;
							case OUT_OF_MEMORY:         // 内存不足
								message = "内存不足";
								break;
							case UNKNOWN:               // 原因不明
								message = "未知错误";
								break;
						}
//						ToastUtils.showToast(context, message, Toast.LENGTH_SHORT);
						SnackBarUtils.makeShort(view,message).show();
					}
					@Override
					public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage){
						float scale = (float) loadedImage.getHeight() / loadedImage.getWidth();
						int screenWidthPixels = DisplayUtils.getScreenWidthPixels(context);
						int screenHeightPixels = DisplayUtils.getScreenHeightPixels(context);
						int height = (int) (screenWidthPixels * scale);
						if(height < screenHeightPixels) {
							height = screenHeightPixels;
						}
						ViewGroup.LayoutParams params = iv_native_preview.getLayoutParams();
						params.height = height;
						params.width = screenWidthPixels;

						iv_native_preview.setImageBitmap(loadedImage);
					}
				});

		container.addView(view);
		return view;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}
	
	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}
}